package be.nepherte.movmanager.server;

import java.util.ArrayList;
import java.util.List;

import be.nepherte.movmanager.client.exceptions.NotLoggedInException;
import be.nepherte.movmanager.client.services.UserServiceAsync;
import be.nepherte.movmanager.domain.Movie;
import be.nepherte.movmanager.domain.UserInfo;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * <p>
 * This class is the server side implementation for user related services.
 * Apart from a server side implementation, a client side asynchronous version,
 * {@link UserServiceAsync}, and a client side stub, 
 * {@link be.nepherte.movmanager.client.services.UserService} is required as well.
 * The used communication protocol is Google's implementation of rpc.
 * </p>
 * 
 * <p>
 * Copyright © 2010 Bart Verhoeven
 * </p>
 * 
 * <p>
 * This file is part of MovManager.
 * </p>
 * 
 * <p>
 * MovManager is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * </p>
 * 
 * <p>
 * MovManager is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * </p>
 * 
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * MovManager. If not, see http://www.gnu.org/licenses/.
 * </p>
 * 
 * @author 	Bart Verhoeven
 * @version 0.1
 */
public class UserServiceImpl extends RemoteServiceServlet implements be.nepherte.movmanager.client.services.UserService {

	private static final long serialVersionUID = 1L;

	@Override
	public UserInfo requestUserInfo(String requestUri) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		UserInfo userInfo;

		// if there is no current user, the person is not logged in
		if (user != null) { 
			userInfo = new UserInfo(user.getUserId(), user.getNickname(), user.getEmail(), true, null, userService.createLogoutURL(requestUri));
			setMovies(userInfo);
		}
		else  {
			userInfo = new UserInfo(null,null,null,false,userService.createLoginURL(requestUri),null);
		}
		
		return userInfo;
	}

	/**
	 * Attaches the movies in the user's movie collection to the info object.
	 * 
	 * @param	userInfo
	 * 			info pertaining to the user
	 */
	private void setMovies(UserInfo userInfo) {
		MovieServiceImpl movieService = new MovieServiceImpl();
		List<Movie> movies;
		
		try {
			movies = movieService.getMovies(userInfo);
		} catch (NotLoggedInException e) {
			// the invocation of getMovies only occurs when we are sure the user is logged in
			movies = new ArrayList<Movie>();
		}
		
		userInfo.setMovies(movies);
	}
}